


#include "main.h"
//#include "adc.h"
//#include "dac.h"
//#include "dma.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"


//这里先重定向一下printf。

#include <stdio.h>

#ifdef __GNUC__									//Serial port redirection
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
PUTCHAR_PROTOTYPE
{
   HAL_UART_Transmit(&huart1 , (uint8_t *)&ch, 1, 0xFFFF);
   return ch;
}



#include"mcu_.h"


unt32 machineTime_ms = 0;
//定时器中断 
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{ 
	if(htim == &htim2)  //Determine whether the interrupt is from timer2
	{
		
		machineTime_ms+=100;
		
  }
}


unt32 mcu_time_ms(void)
{
	return machineTime_ms;
}


void mcu_delay_ms(unt32 k)
{
	HAL_Delay(k);
};



#include"gl_GraphicsDevice.h"

/// 初始化绘图设备
void graphics_init(void){}

void graphics_draw_point(int x, int y, COLOR_RGB color)
{
//	LCD_Draw_Points_ARGB32(x,y,color);
}

void graphics_fill_area(int left, int top, int w, int h, COLOR_RGB color)
{
//	LCD_Draw_Rectangle_ARGB32(left,top,w,h,color);
}

void graphics_draw_image(int left, int top, int w, int h, const COLOR_RGB* color)
{
	for(int i=0;i<h;i++){
		for(int j=0;j<w;j++){
	//		graphics_draw_point(left+j,top+i,color[i*w+j]);		
		}	
	}
}


/// <summary>
/// 判断屏幕是否按下
/// </summary>
/// <param name="px">按下的x坐标</param>
/// <param name="py">按下的y坐标</param>
/// <returns>1 按下；0 未按下</returns>
int GD_is_touched(int* px, int* py){ return 0; }

/// <summary>
/// 等待按键输入，返回按键值。如果没有输入，就返回0.
/// 如果是单片机上，增加一个条件：如果3秒没有按下，就返回0.
/// </summary>
/// <returns></returns>
int GD_wait_key_input(void){ return 0; }


/////////////////////   下面是设备的初始化


	
 char uart0_tx_buff[256];
 char uart1_tx_buff[256];
 char uart2_tx_buff[256];

bool uart0_is_send_finish = true;  //中断是否发送完成
bool uart1_is_send_finish = true;  //中断是否发送完成
bool uart2_is_send_finish = true;  //中断是否发送完成



/// <summary>
/// 串口1发送一个字符串。以空字符'\0'作为字符串结束符。
/// </summary>
/// <param name="str">发送的字符串</param>
void mcu_uart_sendstring(uart* u, const char* str) {
	
	int n=256;
	int i;
	if(u==&uart0){
		while(huart1.gState != HAL_UART_STATE_READY);  //等待串口空闲（发送完成）
		for(i=0;i<n&&str[n]!=0;i++)uart0_tx_buff[i]=str[i];	
		HAL_UART_Transmit_IT(&huart1, (unsigned char*)uart0_tx_buff, n);
	}
//	if(u==&uart1){
//		while(huart2.gState != HAL_UART_STATE_READY);  //等待串口空闲（发送完成）
//		for(i=0;i<n&&str[n]!=0;i++)uart1_tx_buff[i]=str[i];	
//		HAL_UART_Transmit_IT(&huart2, (unsigned char*)uart1_tx_buff, n);
//	}
	if(u==&uart2){
		while(huart3.gState != HAL_UART_STATE_READY);  //等待串口空闲（发送完成）
		for(i=0;i<n&&str[n]!=0;i++)uart2_tx_buff[i]=str[i];	
		HAL_UART_Transmit_IT(&huart3, (unsigned char*)uart2_tx_buff, n);
	}	
	
}

/// <summary>
/// 串口1发送字符串的前n各字符。
/// </summary>
/// <param name="str">发送的字符串</param>
/// <param name="n">字符的个数</param>
void mcu_uart_sendstring_(uart* u, const char* str, int n){
		
	int i;
	if(u==&uart0){
		while(huart1.gState != HAL_UART_STATE_READY);  //等待串口空闲（发送完成）
		for(i=0;i<n;i++)uart0_tx_buff[i]=str[i];	
		HAL_UART_Transmit_IT(&huart1, (unsigned char*)uart0_tx_buff, n);
	}
//	if(u==&uart1){
//		while(huart2.gState != HAL_UART_STATE_READY);  //等待串口空闲（发送完成）
//		for(i=0;i<n;i++)uart1_tx_buff[i]=str[i];	
//		HAL_UART_Transmit_IT(&huart2, (unsigned char*)uart1_tx_buff, n);
//	}
	if(u==&uart2){
		while(huart3.gState != HAL_UART_STATE_READY);  //等待串口空闲（发送完成）
		for(i=0;i<n;i++)uart2_tx_buff[i]=str[i];	
		HAL_UART_Transmit_IT(&huart3, (unsigned char*)uart2_tx_buff, n);
	}	
}
//

unt8 receive_buf_uart0;
unt8 receive_buf_uart2;



//串口接收中断回调函数
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart == &huart1){
		//用队列接收信息。
		deque_strong_push_back(uart0.uart_u8buffer, receive_buf_uart0, char);
		
		//启动新的一次接收
    HAL_UART_Receive_IT(huart, (uint8_t *)&receive_buf_uart0,1);
	}
	if(huart == &huart3){
		//用队列接收信息。		
		deque_strong_push_back(uart2.uart_u8buffer, receive_buf_uart2, char);
		
		//启动新的一次接收
    HAL_UART_Receive_IT(huart, (uint8_t *)&receive_buf_uart2,1);
	}
	
	
}



// 当前的iic设备有：gt911 触摸板，spi有w25q128
// gpio gt_int, gt_rst, gt_sda, gt_scl;  // 分别是 a1,a0,e12,e11,


/// 设置io口的输出值。
void mcu_set_io(gpio* io, unt32 value)
{
	if(io == &gt_int)  HAL_GPIO_WritePin(GPIOE, GPIO_PIN_0, value ? GPIO_PIN_SET : GPIO_PIN_RESET);
	if(io == &gt_rst)  HAL_GPIO_WritePin(GPIOE, GPIO_PIN_1, value ? GPIO_PIN_SET : GPIO_PIN_RESET);
	if(io == &gt_sda)  HAL_GPIO_WritePin(GPIOE, GPIO_PIN_2, value ? GPIO_PIN_SET : GPIO_PIN_RESET);
	if(io == &gt_scl)  HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, value ? GPIO_PIN_SET : GPIO_PIN_RESET);
	
}

/// 读取io口的值。
unt32 mcu_read_io(gpio* io)
{
	if(io == &gt_int) return HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_0); 
	if(io == &gt_rst) return HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_1); 
	if(io == &gt_sda) return HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_2); 
	if(io == &gt_scl) return HAL_GPIO_ReadPin(GPIOE, GPIO_PIN_3);
return 0;	
}


// 将io口设置成输出模式
void mcu_set_io_out(gpio* io)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};

	if(io == &gt_int){ GPIO_InitStruct.Pin = GPIO_PIN_0; }
	if(io == &gt_rst){ GPIO_InitStruct.Pin = GPIO_PIN_1; }
	if(io == &gt_sda){ GPIO_InitStruct.Pin = GPIO_PIN_2; }
	if(io == &gt_scl){ GPIO_InitStruct.Pin = GPIO_PIN_3; }
	
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
	
	
		
}

// 将io口设置成输入模式
void mcu_set_io_in(gpio* io)
{
	GPIO_InitTypeDef GPIO_InitStruct = {0};

	if(io == &gt_int){ GPIO_InitStruct.Pin = GPIO_PIN_0; }
	if(io == &gt_rst){ GPIO_InitStruct.Pin = GPIO_PIN_1; }
	if(io == &gt_sda){ GPIO_InitStruct.Pin = GPIO_PIN_2; }
	if(io == &gt_scl){ GPIO_InitStruct.Pin = GPIO_PIN_3; }
	
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
	
}

//











